package com.erp.erp_servers.Menu.imp;

import com.erp.erp_dao.Menum.IMenuDAO;
import com.erp.erp_dao.Resource.IResourceDAO;
import com.erp.erp_entitys.Menum.MenuEntity2;
import com.erp.erp_entitys.Menum.MenuEntity2Example;
import com.erp.erp_entitys.Resource.ResourceEntity;
import com.erp.erp_entitys.Resource.ResourceEntityExample;
import com.erp.erp_servers.Menu.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ImpResourceService
 * @Description 菜单实现类
 * @Author songjizhen
 * @Date 2020-06-30 22:46
 * @Version 1.0
 */
@Service
public class ImpMenuService implements IMenuService {
    @Autowired
    IMenuDAO menuDAO;
    @Autowired
    IResourceDAO resourceDAO;
    Map<String,String> map=new HashMap<String, String>();
    /**
     * 根据id删除数据
     *
     * @param id
     * @return
     */
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return menuDAO.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteByExample(MenuEntity2Example example) {
        return  menuDAO.deleteByExample(example);
    }

    /**
     * 新增菜单
     *
     * @param record
     * @return
     */
    @Override
    public int insertSelective(MenuEntity2 record) {
        return menuDAO.insertSelective(record);
    }

    /**
     * 更新菜单
     *
     * @param record
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(MenuEntity2 record) {
        return menuDAO.updateByPrimaryKeySelective(record);
    }

    /**
     * 返回菜单树
     *
     * @return
     */
    @Override
    public List<MenuEntity2> getTree() {
       // List<MenuEntity2> selectByExample(MenuEntity2Example example);
        getResource();
        MenuEntity2Example example=new MenuEntity2Example();
        MenuEntity2Example.Criteria criteria=example.createCriteria();
        example.setOrderByClause("xh");
        List<MenuEntity2> menuEntity2s = menuDAO.selectByExample(example);
        MenuEntity2 menu=new MenuEntity2();
        menu.setCode("0");
        menu.setName("根菜单");
        menu.setList(this.initMenu(menuEntity2s,"0"));
        List<MenuEntity2> list=new ArrayList<MenuEntity2>();
        list.add(menu);
        return list;
    }

    /**
     * 检查code是否重复
     *
     * @param code
     * @return
     */
    @Override
    public boolean getExeitBycode(String code) {
        MenuEntity2Example example=new MenuEntity2Example();
        MenuEntity2Example.Criteria criteria=example.createCriteria();
        criteria.andCodeEqualTo(code);
        List<MenuEntity2> menuEntity2s = menuDAO.selectByExample(example);

        return menuEntity2s.size() >0;
    }

    /**
     * 根据条件查询菜单
     *
     * @param example
     * @return
     */
    @Override
    public List<MenuEntity2> selectByExample(MenuEntity2Example example) {
        return  menuDAO.selectByExample(example);
    }

    /**
     * 获取所有资源并存入map
     * @return
     */
    private void getResource(){
        ResourceEntityExample example=new ResourceEntityExample();
    ResourceEntityExample.Criteria criteria = example.createCriteria();
    List<ResourceEntity> resourceEntities = resourceDAO.selectByExample(example);
    for(int i=0;i<resourceEntities.size();i++){
        String url=resourceEntities.get(i).getUrl();
        String name=resourceEntities.get(i).getName();
        //if(url !=null && url !="" )map.put(url,name);
        String code=resourceEntities.get(i).getCode();
        map.put(code,url);
    }
}

    private List<MenuEntity2>  initMenu(List<MenuEntity2> menus ,String pid){
        List<MenuEntity2> list=new ArrayList<MenuEntity2>();

        for(int i=0;i<menus.size();i++){
            if(menus.get(i).getPid().equals(pid)){
                MenuEntity2 menu=new MenuEntity2();
                menu.setName(menus.get(i).getName());
                menu.setCode(menus.get(i).getCode());
                menu.setId(menus.get(i).getId());
                menu.setLevel(menus.get(i).getLevel());
                menu.setPath(menus.get(i).getPath());
                //menu.setUrl(menus.get(i).getUrl());
                menu.setUrl(map.get(menus.get(i).getUrl()));
                menu.setPid(menus.get(i).getPid());
                menu.setList(this.initMenu(menus,menus.get(i).getCode()));
                list.add(menu);
            }
        }
        return list;
    }
}
